import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author huu
 * @create 2025-09-06-15:22
 */
public class spiralOrder {
    public List<Integer> spiralOrder(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        int left = 0;
        int right = n - 1;
        int top = 0;
        int bottom = m - 1;
        int direction = 0;
        List<Integer> res = new ArrayList<>();
        while(true){
            for(int i = left; i <= right; i++) res.add(matrix[top][i]);//向右
            if(++top > bottom) break;
            for(int i = top; i <= bottom; i++) res.add(matrix[i][right]);//向下
            if(--right < left) break;
            for(int i = right; i >= left; i--) res.add(matrix[bottom][i]);//向左
            if(--bottom < top) break;
            for(int i = bottom; i >= top; i--) res.add(matrix[i][left]);//向上
            if (++left > right) break;
        }
        return res;
    }

}
